<?php

  require_once("setup.inc.php");

  if(isset($_REQUEST['submit'])) {
  
    //submit / save redstar data..
	$tmpfname = tempnam("/export/www/transportation/redstars", "complete");
	$redstarfile = fopen($tmpfname, 'w');
	$beginningText = "Redstar List Completed on " . date("F j, Y, g:i a") . "\nCompleted by " . $_SESSION['firstname'] . " " . $_SESSION['lastname'] . "\n\nShift: " . $_REQUEST['shift_line'] . " from " . $_REQUEST['shift_start'] . " to " . $_REQUEST['shift_end'] . " on " . $_REQUEST['shift_date'] . "\n\nAdditional comments from driver:\n" . $_REQUEST['additionalComments'] . "\n\nRedstar list results:\n\n  Driver Name            Response               Date and Time\n----------------------------------------------------------------\n";
	fwrite($redstarfile,$beginningText);		
	if(isset($_REQUEST['1_name']))
		$moredata = true;
	$count = 1;
	while($moredata) {
		$name = $count . '_name';
		$date = $count . '_date';
		$time = $count . '_time';
		$response = $count . '_response';
		fwrite($redstarfile,"| " .substr(str_pad($_REQUEST[$name],20),0,20) . " | " . substr(str_pad($_REQUEST[$response],20),0,20) . " | " . substr(str_pad($_REQUEST[$date],8),0,8) . ' ' . substr(str_pad($_REQUEST[$time],5),0,5) . " |\n");
		$count ++;
		$newname = $count . '_name';
		if(isset($_REQUEST[$newname])) {
			$moredata = true;
		} else {
			$moredata = false;
		}
	}
	fwrite($redstarfile,"----------------------------------------------------------------");
	fclose($redstarfile);
	//add redstar to redstar table
	$sql = "INSERT INTO `t_redstars` ( `redstar_id` , `driver_id` , `status` , `date` , `time` , `list_file_name` ) 
			VALUES ('0', '" . $_SESSION['driver_id'] . "', '0', '" . date("Ymd") . "', '" . date("Hi") . "', '" . $tmpfname . "')";
	$insert = mysql_query($sql);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>University of Connecticut - Department of Transportation Services</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.mainTitle {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 16px;
	font-style: normal;
	font-weight: bold;
	text-align: right;
}
.pageTitle {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
	font-style: normal;
	font-weight: bold;
	text-align: center;
}
.descriptionTitle {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-style: normal;
	font-weight: bold;
	text-align: center;
}
.descriptionText {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	font-style: normal;
	font-weight: none;
	text-align: justify;
}
.listText {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	font-style: normal;
	font-weight: none;
	text-align: left;
}
td.listText {
	border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width="600" border="0" align="center">
  <tr style="border-bottom: medium solid black;"> 
    <td width="190"><img src="images/uconn_logo.jpg" width="170" height="44"></td>
    <td width="404" class="mainTitle">Transportation Services</td>
  </tr>
  <tr> 
    <td colspan="2"><p class="pageTitle">Red Star Phone List</p>
		
				<?php if(mysql_affected_rows()) { ?>
					<p style="text-align: center;" class="listText">Your redstar list has been submitted.</p>
				<?php } else { ?>
					<p style="text-align: center; font-weight:bold;" class="listText">An error occurred while saving the list. Please print this and bring it to base.</p>
					<pre><?php include($tmpfname); ?></pre>
				<?php }	?>
		<p style="text-align: center;" class="listText"><input class="listText" style="text-align:center;" type="button" value="Close this window" onClick="self.close();"></p>
	</td>
  </tr>
</table>
</body>
</html>
<?php
	exit;
  } // end sumbit if

  switch ($_REQUEST['shift']) {
  	case 'BW':
		$permissionLevel = '0002';
		$shiftTitle = "Bus Wash";
		break;	
	case 'S':
		$permissionLevel = '0004';
		$shiftTitle = "Shuttle";
		break;
	case 'D':
		$permissionLevel = '0400';
		$shiftTitle = "Dispatch";
		break;
  }
  
  $sql = "SELECT * FROM `t_drivers` WHERE `terminated` IS NULL AND `durational` = 0 ORDER BY `permission` ASC, `lastname` ASC, `firstname` ASC";
  $result = mysql_query($sql);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>University of Connecticut - Department of Transportation Services</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.mainTitle {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 16px;
	font-style: normal;
	font-weight: bold;
	text-align: right;
}
.pageTitle {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
	font-style: normal;
	font-weight: bold;
	text-align: center;
}
.descriptionTitle {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-style: normal;
	font-weight: bold;
	text-align: center;
}
.descriptionText {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	font-style: normal;
	font-weight: none;
	text-align: justify;
}
.listText {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	font-style: normal;
	font-weight: none;
	text-align: left;
}
td.listText {
	border: thin solid #000000;
}
-->
</style>
</head>
<script type="text/javascript" language="JavaScript">
<!-- hide from older browsers

function updateTime (id) {
 today = new Date()
 nowHours = today.getHours()
 nowMinutes = today.getMinutes()
 if (nowHours < 10) {
	nowHours = '0' + today.getHours()
 } else {
	nowHours = today.getHours()
 }
 
 if (nowMinutes < 10) {
	nowMinutes = '0' + today.getMinutes()
 } else {
	nowMinutes = today.getMinutes()
 }
 document.getElementById(id + '_time').value = nowHours + ':' + nowMinutes
}

// -->
</script>

<body>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<table width="600" border="0" align="center">
  <tr style="border-bottom: medium solid black;"> 
    <td width="190"><img src="images/uconn_logo.jpg" width="170" height="44"></td>
    <td width="404" class="mainTitle">Transportation Services</td>
  </tr>
  <tr> 
    <td colspan="2"><p class="pageTitle">Red Star Phone List</p>
      <p class="descriptionTitle"><?= $shiftTitle ?> Shift</p></td>
  </tr>
  <tr> 
    <td colspan="2"><p class="descriptionText">Check off each person after asking 
        him/her to cover your shift. When completed, sign and date on the back. 
        This form must be completed and turned into the AM of Ops prior to the 
        red-starred shift, if it remains uncovered at that time. This list has 
        been customized to only have drivers for the type of shift listed above. 
        If you require a different Red Star list, please log on to <a href="http://tranpso.uconn.edu/">http://tranpso.uconn.edu/</a>.</p>
      </td>
  </tr>
  <tr> 
    <td colspan="2" class="listText"> 
      <table width="600" >
        <thead>
          <tr> 
            <th width="182">Name:</th>
            <th width="120">Phone:</th>
            <th width="65">Date:</th>
            <th width="50">Time:</th>
            <th width="134">Response:</th>
          </tr>
        </thead>
		<tbody>
<?php 
	$count = 0;
	while($row = mysql_fetch_assoc($result)) {
		if(!checkQualified($permissionLevel,$row['permission']))
			continue;
		$count ++;
?>
        <tr class="listText"> 
          <td class="listText"><?php print $row['lastname'] . ", " . $row['firstname']; ?></td>
          <td class="listText" style="text-align: center;"><?php
		  				switch ($row['redstarphone']) {
							case 'c': print formatPhone($row['cellphone']); break;
							case 'l': print formatPhone($row['campusphone']); break;
							case 'h': print formatPhone($row['homephone']); break;
							default: print "No Number"; break;
						}
		   ?></td>
		  <input type="hidden" name="<?= $count ?>_name" value="<?php print $row['lastname'] . ", " . $row['firstname']; ?>">
          <td class="listText" align="center"><input type="text" class="listText" style="text-align: center; width: 65px;" name="<?= $count ?>_date" id="<?= $count ?>_date" value="<?php echo date("m/d/y"); ?>"></td>
          <td class="listText" align="center"><input type="text" class="listText" style="text-align: center; width: 50px;" name="<?= $count ?>_time" id="<?= $count ?>_time" onFocus="updateTime(<?= $count ?>)" value=""></td>
          <td class="listText" align="center"><input type="text" class="listText" style="text-align: left;   width: 150px;" name="<?= $count ?>_response" id="<?= $count ?>_response" value=""></td>
        </tr>
<?php
	}
?>
        <tr class="listText"> 
          <td colspan="5" class="listText" style="text-align: center; border:none;" align="center"><hr></td>
        </tr>
        <tr class="listText"> 
          <td colspan="5" class="listText" style="text-align: center; padding: 5px;" align="center"><div class="listText" style="font-weight:bold; text-align: center; padding: 0px 0px 5px 0px;">Red-starred Shift Information</div>
              <div class="listText" style="font-weight:normal; text-align: center; padding: 0px 0px 5px 0px;">Shift 
                (ex. Blue 1) 
                <input type="text" class="listText" style="text-align: center; width: 65px;" name="shift_line" id="shift_line" value="">
                Shift Date 
                <input type="text" class="listText" style="text-align: center; width: 65px;" name="shift_date" id="shift_date" value="">
              </div>Start Time: <input type="text" name="shift_start" class="listText" style="width:50px; text-align: center;"> End Time: <input type="text" name="shift_end" class="listText" style="width:50px; text-align: center;"></td>
        </tr>
        <tr class="listText">
          <td colspan="5" class="listText" style="text-align: center; padding: 5px;" align="center">
		    <div class="listText" style="font-weight:bold; text-align: center; padding: 0px 0px 5px 0px;">Additional Comments</div>
			<div class="listText" align="center" style="text-align:center; "><textarea name="additionalComments" class="listText" style="width: 550px;  "></textarea></div>
		  </td>
        </tr>
        <tr class="listText">
          <td colspan="5" class="listText" style="text-align: center; padding: 5px;" align="center"><input type="submit" class="listText" style="text-align:center; " name="submit" value="Submit Redstar List"></td>
        </tr>
		</tbody>
      </table></td>
  </tr>
</table>
</form>
</body>
</html>
